package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemDao dao;

    @Override
    public void add(CheckItem checkItem) {

        dao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page =  dao.findByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void delById(Integer id) {
        long count =  dao.findCheckitemCountById(id);
        if (count>0){

            throw new RuntimeException("该项在检查组中存在,不能删除!!");
        }else {
            dao.delById(id);
        }


    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = dao.findById(id);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        dao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {

        return dao.findAll();
    }
}
